<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>新增页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form" action="">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <input type="hidden" name="source_id" value="<?php echo $info['id']; ?>" />
                    <input type="hidden" name="is_folder" value="<?php echo $info['is_folder']; ?>" />
                    <input type="hidden" name="file_id" value="<?php echo $info['file_id']; ?>" />
                    <div class="layui-form-item">
                        <label class="layui-form-label">分享名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" lay-verify="required" value="<?php echo $info['title']; ?>" placeholder="分享名称不能为空" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">有效期</label>
                        <div class="layui-input-block">
                            <input type="radio" name="expire_at" value="1" title="1天" checked>
                            <input type="radio" name="expire_at" value="7" title="7天">
                            <input type="radio" name="expire_at" value="30" title="1个月">
                            <input type="radio" name="expire_at" value="0" title="永久">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">提取码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="share_password" value="" placeholder="为空则不设置提取码" class="layui-input">
                        </div>
                        <div class=" layui-word-aux">
                            <button class="pear-btn pear-btn-primary pear-btn-sm" type="button" id="random_paw">
                                随机
                            </button>
                        </div>
                    </div>

                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                        lay-filter="save">
                        生成分享外链
                    </button>
                    <button type="button" class="pear-btn pear-btn-md layer-close">
                        关闭
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/Sortable.js"></script>
        <script src="/app/admin/admin/js/all.js"></script>
        
        <script>

            // 相关接口
            const INSERT_API = "/app/netdisk/admin/io-source/share";

            // 字段 创建时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    type: "datetime",
                });
            })
            
            // 字段 更新时间 updated_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#updated_at",
                    type: "datetime",
                });
            })
            
            // 字段 状态 status
            layui.use(["form"], function() {
                layui.$("#status").attr("checked", layui.$('input[name="status"]').val() != 0);
                layui.form.render();
                layui.form.on("switch(status)", function(data) {
                    layui.$('input[name="status"]').val(this.checked ? 1 : 0);
                });
            })
            
            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    layui.$.ajax({
                        url: INSERT_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.shareFrame("<?php echo $info['id']; ?>","<?php echo $info['title']; ?>");
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });

                        }
                    });
                    return false;
                });
                //关闭
                layui.$(".layer-close").click(function(){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
                //生成随机密码
                layui.$("#random_paw").click(function(){
                    layui.$("input[name='share_password']").val(generateRandomCode())
                });
                //随机码生成
                let generateRandomCode = function() {
                    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                    var code = '';
                    for (var i = 0; i < 6; i++) {
                        var randomIndex = Math.floor(Math.random() * characters.length);
                        // 如果是第一位，且随机到的字符是0，则重新随机一个字符
                        if (i === 0 && characters.charAt(randomIndex) === '0') {
                            i--;
                            continue;
                        }
                        code += characters.charAt(randomIndex);
                    }
                    return code;
                }
            });

        </script>

    </body>
</html>
